/*  HostMon/msntop - A simple program to monitor hosts on android
	Copyright (C) 2009 Devendra Laulkar

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation version 2 of the License. 

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.android.msntop;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class HostAdd extends Activity {

	private static final int HELP = 0;
	private boolean inEdit = false;

	public void onCreate(Bundle savedInstance)
	{
		super.onCreate(savedInstance);
		setContentView(R.layout.host_add);
		Button add = (Button) findViewById(R.id.add_button);

		final EditText host = (EditText) findViewById(R.id.add_host);
		final TextView comment = (TextView) findViewById(R.id.add_comment);
		final TextView sshUser = (TextView) findViewById(R.id.add_sshuser);
		final TextView sshPort = (TextView) findViewById(R.id.add_sshport);
		final TextView optPath = (TextView) findViewById(R.id.add_optpath);
		
		comment.setNextFocusDownId(R.id.add_sshuser);
		Bundle extras = getIntent().getExtras();

		if(extras != null)
		{
			host.setText(extras.getString(Host.NAME));
			host.setFocusable(false);
			comment.setText(extras.getString(Host.COMMENT));
			sshUser.setText(extras.getString(Host.SSHUSER));
			sshPort.setText(extras.getString(Host.SSHPORT));

			host.setEnabled(false);
			add.setText(R.string.view_edit_btn);
			inEdit = true;
		}

		add.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {

				// TODO - Check if host is already present
				Intent mIntent = new Intent();
				if("".equals(host.getText().toString()))
				{
					setResult(RESULT_CANCELED, mIntent);
					finish();
				}
				Bundle bundle = new Bundle();

				bundle.putString(Host.NAME, host.getText().toString());
				bundle.putString(Host.COMMENT, comment.getText().toString());
				bundle.putString(Host.SSHUSER, sshUser.getText().toString());
				bundle.putString(Host.SSHPORT, sshPort.getText().toString());

				mIntent.putExtras(bundle);
				setResult(RESULT_OK, mIntent);
				finish();
			}
		});
	}

	private void displayHelp() {

		new AlertDialog.Builder(this).setMessage(R.string.help_add)
		.setPositiveButton(android.R.string.ok, null).show();
	}
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		MenuItem item;
		item = menu.add(0, HELP,0, R.string.help);
		item.setIcon(android.R.drawable.ic_menu_help);

		return true;
	}

	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		displayHelp();
		return super.onMenuItemSelected(featureId, item);

	}
}
